package com.wyg.listener;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import com.wyg.pojo.User;

public class SessionListener implements HttpSessionListener {

	Map<String,User> useMap=null;
	HttpSession session=null;
	ServletContext sd=null;
	@SuppressWarnings("unchecked")
	@Override
	public void sessionCreated(HttpSessionEvent event) {
		session = event.getSession();
		sd = session.getServletContext();
		useMap = (Map<String, User>) sd.getAttribute("user");
		
		if(useMap == null){
			useMap = new HashMap<String,User>();
			sd.setAttribute("user", useMap);
		}

	}

	@Override
	public void sessionDestroyed(HttpSessionEvent event) {
		((Map<String, User>) sd.getAttribute("user")).remove(event.getSession().getId());
	}

}
